
#include "../includes.h"


TR_	tr1;

char	hex_char[16] = "0123456789ABCDEF";

char	REFERENCE_MODE;
char	PREV_REFERENCE_MODE;

void Init_MAINFUNC(void);
void CheckLock();

void AverageTemperature(float);
void AverageCurrent(float);
void gwait(int);

/* ----------------------------------------------------------- */
//    CONFIG
/* ----------------------------------------------------------- */
void WriteCONFIG();
void PresetCONFIG();
char ReadCONFIG();


/* ----------------------------------------------------------- */
//    CRC16
/* ----------------------------------------------------------- */
unsigned short ChekcCRC16(unsigned char *,int);



int gps_holdover_counter;
int lmu_holdover_counter;
int e1_holdover_counter;
int e2_holdover_counter;

char gps_detect_flag;
char lmu_detect_flag;
char e1_detect_flag;
char e2_detect_flag;

char gps_lock_flag;
char lmu_lock_flag;
char e1_lock_flag;
char e2_lock_flag;


char prev_current_sensor_check;
char current_sensor_check;
int	 censor_check_counter;
int	 censor_check_counter2;



typedef union
{
        float value;
        unsigned int word;
}ieee_float_shape_type;


/*little endian and big endian diff*/
typedef union
{
        double value;
        struct
        {
        unsigned int msw;
        unsigned int  lsw;
        } parts;
} ieee_double_shape_type;


int floatNaN(float x)
{

        int ix;
        do{
                ieee_float_shape_type gf_u;
                gf_u.value = (x);
                (ix) = gf_u.word;
        }while(0);

        ix &= 0x7fffffff;
        ix = 0x7f800000 - ix;

        return (int)(((unsigned int)(ix))>>31);

}

int doubleNaN(double x)
{

        int hx,lx;
        do{
                ieee_double_shape_type ew_u;
                ew_u.value = (x);
                (hx) = ew_u.parts.lsw;
                (lx) = ew_u.parts.msw;
        }while(0);
          hx &= 0x7fffffff;
          hx |= (unsigned int)(lx|(-lx))>>31;
          hx = 0x7ff00000 - hx;

          return (int)(((unsigned int)hx)>>31);

}


unsigned short CRC16TABLE[256] =
{
	0x0000, 0xC0C1, 0xC181, 0x0140, 0xC301, 0x03C0, 0x0280, 0xC241,
	0xC601, 0x06C0, 0x0780, 0xC741, 0x0500, 0xC5C1, 0xC481, 0x0440,
	0xCC01, 0x0CC0, 0x0D80, 0xCD41, 0x0F00, 0xCFC1, 0xCE81, 0x0E40,
	0x0A00, 0xCAC1, 0xCB81, 0x0B40, 0xC901, 0x09C0, 0x0880, 0xC841,
	0xD801, 0x18C0, 0x1980, 0xD941, 0x1B00, 0xDBC1, 0xDA81, 0x1A40,
	0x1E00, 0xDEC1, 0xDF81, 0x1F40, 0xDD01, 0x1DC0, 0x1C80, 0xDC41,
	0x1400, 0xD4C1, 0xD581, 0x1540, 0xD701, 0x17C0, 0x1680, 0xD641,
	0xD201, 0x12C0, 0x1380, 0xD341, 0x1100, 0xD1C1, 0xD081, 0x1040,
	0xF001, 0x30C0, 0x3180, 0xF141, 0x3300, 0xF3C1, 0xF281, 0x3240,
	0x3600, 0xF6C1, 0xF781, 0x3740, 0xF501, 0x35C0, 0x3480, 0xF441,
	0x3C00, 0xFCC1, 0xFD81, 0x3D40, 0xFF01, 0x3FC0, 0x3E80, 0xFE41,
	0xFA01, 0x3AC0, 0x3B80, 0xFB41, 0x3900, 0xF9C1, 0xF881, 0x3840,
	0x2800, 0xE8C1, 0xE981, 0x2940, 0xEB01, 0x2BC0, 0x2A80, 0xEA41,
	0xEE01, 0x2EC0, 0x2F80, 0xEF41, 0x2D00, 0xEDC1, 0xEC81, 0x2C40,
	0xE401, 0x24C0, 0x2580, 0xE541, 0x2700, 0xE7C1, 0xE681, 0x2640,
	0x2200, 0xE2C1, 0xE381, 0x2340, 0xE101, 0x21C0, 0x2080, 0xE041,
	0xA001, 0x60C0, 0x6180, 0xA141, 0x6300, 0xA3C1, 0xA281, 0x6240,
	0x6600, 0xA6C1, 0xA781, 0x6740, 0xA501, 0x65C0, 0x6480, 0xA441,
	0x6C00, 0xACC1, 0xAD81, 0x6D40, 0xAF01, 0x6FC0, 0x6E80, 0xAE41,
	0xAA01, 0x6AC0, 0x6B80, 0xAB41, 0x6900, 0xA9C1, 0xA881, 0x6840,
	0x7800, 0xB8C1, 0xB981, 0x7940, 0xBB01, 0x7BC0, 0x7A80, 0xBA41,
	0xBE01, 0x7EC0, 0x7F80, 0xBF41, 0x7D00, 0xBDC1, 0xBC81, 0x7C40,
	0xB401, 0x74C0, 0x7580, 0xB541, 0x7700, 0xB7C1, 0xB681, 0x7640,
	0x7200, 0xB2C1, 0xB381, 0x7340, 0xB101, 0x71C0, 0x7080, 0xB041,
	0x5000, 0x90C1, 0x9181, 0x5140, 0x9301, 0x53C0, 0x5280, 0x9241,
	0x9601, 0x56C0, 0x5780, 0x9741, 0x5500, 0x95C1, 0x9481, 0x5440,
	0x9C01, 0x5CC0, 0x5D80, 0x9D41, 0x5F00, 0x9FC1, 0x9E81, 0x5E40,
	0x5A00, 0x9AC1, 0x9B81, 0x5B40, 0x9901, 0x59C0, 0x5880, 0x9841,
	0x8801, 0x48C0, 0x4980, 0x8941, 0x4B00, 0x8BC1, 0x8A81, 0x4A40,
	0x4E00, 0x8EC1, 0x8F81, 0x4F40, 0x8D01, 0x4DC0, 0x4C80, 0x8C41,
	0x4400, 0x84C1, 0x8581, 0x4540, 0x8701, 0x47C0, 0x4680, 0x8641,
	0x8201, 0x42C0, 0x4380, 0x8341, 0x4100, 0x81C1, 0x8081, 0x4040
};

/* Table of CRC constants - implements x^16+x^12+x^5+1 */
unsigned short crc16_tab[] = {
    0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7,
    0x8108, 0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef,
    0x1231, 0x0210, 0x3273, 0x2252, 0x52b5, 0x4294, 0x72f7, 0x62d6,
    0x9339, 0x8318, 0xb37b, 0xa35a, 0xd3bd, 0xc39c, 0xf3ff, 0xe3de,
    0x2462, 0x3443, 0x0420, 0x1401, 0x64e6, 0x74c7, 0x44a4, 0x5485,
    0xa56a, 0xb54b, 0x8528, 0x9509, 0xe5ee, 0xf5cf, 0xc5ac, 0xd58d,
    0x3653, 0x2672, 0x1611, 0x0630, 0x76d7, 0x66f6, 0x5695, 0x46b4,
    0xb75b, 0xa77a, 0x9719, 0x8738, 0xf7df, 0xe7fe, 0xd79d, 0xc7bc,
    0x48c4, 0x58e5, 0x6886, 0x78a7, 0x0840, 0x1861, 0x2802, 0x3823,
    0xc9cc, 0xd9ed, 0xe98e, 0xf9af, 0x8948, 0x9969, 0xa90a, 0xb92b,
    0x5af5, 0x4ad4, 0x7ab7, 0x6a96, 0x1a71, 0x0a50, 0x3a33, 0x2a12,
    0xdbfd, 0xcbdc, 0xfbbf, 0xeb9e, 0x9b79, 0x8b58, 0xbb3b, 0xab1a,
    0x6ca6, 0x7c87, 0x4ce4, 0x5cc5, 0x2c22, 0x3c03, 0x0c60, 0x1c41,
    0xedae, 0xfd8f, 0xcdec, 0xddcd, 0xad2a, 0xbd0b, 0x8d68, 0x9d49,
    0x7e97, 0x6eb6, 0x5ed5, 0x4ef4, 0x3e13, 0x2e32, 0x1e51, 0x0e70,
    0xff9f, 0xefbe, 0xdfdd, 0xcffc, 0xbf1b, 0xaf3a, 0x9f59, 0x8f78,
    0x9188, 0x81a9, 0xb1ca, 0xa1eb, 0xd10c, 0xc12d, 0xf14e, 0xe16f,
    0x1080, 0x00a1, 0x30c2, 0x20e3, 0x5004, 0x4025, 0x7046, 0x6067,
    0x83b9, 0x9398, 0xa3fb, 0xb3da, 0xc33d, 0xd31c, 0xe37f, 0xf35e,
    0x02b1, 0x1290, 0x22f3, 0x32d2, 0x4235, 0x5214, 0x6277, 0x7256,
    0xb5ea, 0xa5cb, 0x95a8, 0x8589, 0xf56e, 0xe54f, 0xd52c, 0xc50d,
    0x34e2, 0x24c3, 0x14a0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
    0xa7db, 0xb7fa, 0x8799, 0x97b8, 0xe75f, 0xf77e, 0xc71d, 0xd73c,
    0x26d3, 0x36f2, 0x0691, 0x16b0, 0x6657, 0x7676, 0x4615, 0x5634,
    0xd94c, 0xc96d, 0xf90e, 0xe92f, 0x99c8, 0x89e9, 0xb98a, 0xa9ab,
    0x5844, 0x4865, 0x7806, 0x6827, 0x18c0, 0x08e1, 0x3882, 0x28a3,
    0xcb7d, 0xdb5c, 0xeb3f, 0xfb1e, 0x8bf9, 0x9bd8, 0xabbb, 0xbb9a,
    0x4a75, 0x5a54, 0x6a37, 0x7a16, 0x0af1, 0x1ad0, 0x2ab3, 0x3a92,
    0xfd2e, 0xed0f, 0xdd6c, 0xcd4d, 0xbdaa, 0xad8b, 0x9de8, 0x8dc9,
    0x7c26, 0x6c07, 0x5c64, 0x4c45, 0x3ca2, 0x2c83, 0x1ce0, 0x0cc1,
    0xef1f, 0xff3e, 0xcf5d, 0xdf7c, 0xaf9b, 0xbfba, 0x8fd9, 0x9ff8,
    0x6e17, 0x7e36, 0x4e55, 0x5e74, 0x2e93, 0x3eb2, 0x0ed1, 0x1ef0,
};

unsigned short calc_crc16(unsigned char *buf, int len)
{
    int i;
    unsigned short cksum;

    cksum = 0;
    for (i = 0;  i < len;  i++) {
        cksum = crc16_tab[((cksum>>8) ^ *buf++) & 0xFF] ^ (cksum << 8);
    }
    return cksum;
}

void Init_MAINFUNC(void)
{
    int i,j;

	//WarmupTimeCounter = 1*60;

    TEMPERATURE.Counter =   0;
    TEMPERATURE.Low =   0;
    TEMPERATURE.Mid =   0;
    TEMPERATURE.Mid2 =  0;
    TEMPERATURE.High =  0;

    /* TEMPERATURE */
    TEMPERATURE.CURRENT    = 0;
	TEMPERATURE.Turn = TEMPER_SENSOR;

   	TEMPERATURE.InsertCounter  = 0;
	TEMPERATURE.AVERAGE = 0;

    for( i = 0; i < TEMPERATURE_AVERAGE_INTERVAL; i++){
        TEMPERATURE.AverageTable[i] = 0;
    }

	CHECKLOCK.LockedCounter = 0;
	CHECKLOCK.UnlockedCounter = 0;

	CHECKLOCK.HoldoverDelayCounter = 0;

	REFERENCE_MODE = UNDEFINED;
	Select1PPS8K1();

    for( i = 0; i < CURRENT_AVERAGE_INTERVAL; i++){
        TEMPERATURE.CurrentAverageTable[i] = 0;
    }

    TEMPERATURE.current    = 0.001;
    TEMPERATURE.current_average    = 0.001;
   	TEMPERATURE.CurrentInsertCounter  = 0;

	prev_current_sensor_check = OK;
	current_sensor_check = OK;
	censor_check_counter = 0;
	censor_check_counter2 = 0;

	gps_holdover_counter = 0;
	lmu_holdover_counter = 0;
	e1_holdover_counter = 0;
	e2_holdover_counter = 0;

	gps_detect_flag = OFF;
	lmu_detect_flag = OFF;
	e1_detect_flag = OFF;
	e2_detect_flag = OFF;

	gps_lock_flag = OFF;
	lmu_lock_flag = OFF;
	e1_lock_flag = OFF;
	e2_lock_flag = OFF;

	Select1PPSEb();
}


void AverageTemperature(float t)
{
	int i, index;

	float fsum;

	TEMPERATURE.InsertCounter++;

	index = TEMPERATURE.InsertCounter % TEMPERATURE_AVERAGE_INTERVAL;

	TEMPERATURE.AverageTable[index] = t;

	index=(TEMPERATURE.InsertCounter>TEMPERATURE_AVERAGE_INTERVAL)?TEMPERATURE_AVERAGE_INTERVAL:TEMPERATURE.InsertCounter;


	for( i = 0, fsum = 0; i < index; i++ ){
		fsum += TEMPERATURE.AverageTable[i];
	}
	
	TEMPERATURE.AVERAGE = fsum/(float)index;


	if( SYSTEM.OPERATION.OPMODE != OPMODE_POWERUP){
		if(OCXO_ID.Valid == YES ){ GetTempDAC(); }
	}

}


void AverageCurrent(float t)
{
	int i, index;

	float fsum;

	TEMPERATURE.CurrentInsertCounter++;

	index = TEMPERATURE.CurrentInsertCounter % CURRENT_AVERAGE_INTERVAL;

	TEMPERATURE.CurrentAverageTable[index] = t;

	index=(TEMPERATURE.CurrentInsertCounter>CURRENT_AVERAGE_INTERVAL)?CURRENT_AVERAGE_INTERVAL:TEMPERATURE.CurrentInsertCounter;


	for( i = 0, fsum = 0; i < index; i++ ) {
		fsum += TEMPERATURE.CurrentAverageTable[i];
	}
	TEMPERATURE.current_average = fsum/(float)index;

/*
	if( SYSTEM.OPERATION.OPMODE != OPMODE_POWERUP){
		if(OCXO_ID.Valid == YES ){	GetTempDAC();}
	}
*/
}


char is_holdover_condition()
{
	switch(REFERENCE_MODE) {

		case	EB_STATUS_RUN:
								if( gps_holdover_counter > 0 ){
									if( gps_holdover_counter > Holdover_limit ){
										return HOLDOVER_OVER10MIN;
									}
									else {
										return HOLDOVER;
									}
								}
								else {
									return LOCKED;
								}

								break;
		case	E1_STATUS_RUN:
								if( e1_holdover_counter > 0 ){
									if( e1_holdover_counter > Holdover_limit ){
										return HOLDOVER_OVER10MIN;
									}
									else {
										return HOLDOVER;
									}
								}
								else {
									return LOCKED;
								}
								break;
		case	E2_STATUS_RUN:
								if( e2_holdover_counter > 0 ){
									if( e2_holdover_counter > Holdover_limit ){
										return HOLDOVER_OVER10MIN;
									}
									else {
										return HOLDOVER;
									}
								}
								else {
									return LOCKED;
								}
								break;
		case	LMU_STATUS_RUN:
								if( lmu_holdover_counter > 0 ){
									if( lmu_holdover_counter > Holdover_limit ){
										return HOLDOVER_OVER10MIN;
									}
									else {
										return HOLDOVER;
									}
								}
								else {
									return LOCKED;
								}
								break;
		default:
				return HOLDOVER_OVER10MIN;
				break;
	}
	return HOLDOVER_OVER10MIN;
}


void CheckLock()
{

	char	parity_error;
	char	mode_error;

    int i,FaultCounter,LockCounter,sat_tracked;

    int j;
    unsigned int tt;

	char t1,t2,t3,t4,t5,t6,t7;

	char	holdover_condition;
	char	position_hold_plus;

	if(SYSTEM.EB.SELFTEST == OK){
		if(SYSTEM.TRACK.DOPTYPE&0x80) {
			if(CONFIG.AntCheckFlag == ON)
		    {
			   	SYSTEM.EB.ANTENNA = ANTENNA_OPENED;
				/*if( SYSTEM.EB.PrevANTENNA != ANTENNA_OPENED ){  	LogIt(LOG_Antenna_Open,0);	}*/
			}
			DIAG.AntennaFaultCounter++;
		}
		// short
		else if(SYSTEM.TRACK.DOPTYPE&0x40){
			if(CONFIG.AntCheckFlag == ON)
		    {
				SYSTEM.EB.ANTENNA = ANTENNA_SHORTED;
				/*if( SYSTEM.EB.PrevANTENNA != ANTENNA_SHORTED ){  	LogIt(LOG_Antenna_Short,0);	}*/
			}
			DIAG.AntennaFaultCounter++;
		}
		// normal
		else {
			SYSTEM.EB.ANTENNA = OK;
			DIAG.AntennaFaultCounter = 0;
		}
	}
        
	SYSTEM.EB.PrevANTENNA = SYSTEM.EB.ANTENNA;


	//if(CONFIG.AntCheckFlag) {
	//	/*--------------------------------------------------
	//	ANTENNA FAULT  (fault at minimum 5 seconds successive fault )
	//	---------------------------------------------------*/
	//	// open
	//	if(SYSTEM.TRACK.DOPTYPE&0x80) 		{CHECKLOCK.AntennaFaultTable[GC.Second%2] = -1;	}
	//	// short
	//	else if(SYSTEM.TRACK.DOPTYPE&0x40) 	{CHECKLOCK.AntennaFaultTable[GC.Second%2] = 1;	}
	//	else 								{CHECKLOCK.AntennaFaultTable[GC.Second%2] = 0;	}
	//	
	//	
	//	for( i = 0,FaultCounter = 0; i < 2; i++)  FaultCounter += CHECKLOCK.AntennaFaultTable[i];
	//	
	//	switch(FaultCounter){
	//		case	0:
	//					SYSTEM.EB.ANTENNA = OK;
	//		        	DIAG.AntennaFaultCounter = 0;
	//					break;
	//		case	2:
	//					SYSTEM.EB.ANTENNA = ANTENNA_SHORTED;
	//			        if( SYSTEM.EB.PrevANTENNA == OK ){
	//			        	LogIt(LOG_Antenna_Short,0);
	//			        }
	//		        	DIAG.AntennaFaultCounter++;
	//					break;
	//		case	-2:	// Antenna Open � (LG)
	//					SYSTEM.EB.ANTENNA = ANTENNA_OPENED;
	//			        if( SYSTEM.EB.PrevANTENNA == OK ){
	//			        	LogIt(LOG_Antenna_Open,0);
	//			        }
	//		        	DIAG.AntennaFaultCounter++;
	//					break;
	//	}
	//	
	//	SYSTEM.EB.PrevANTENNA = SYSTEM.EB.ANTENNA;
	//}
		
	if(SYSTEM.EB.STATUS != EB_STATUS_RUN){
		//SYSTEM.EB.LOCKMODE = HOLDOVER_OVER10MIN;
		//CHECKLOCK.HoldoverCounter++;
		CHECKLOCK.LockedCounter = 0;
		CHECKLOCK.UnlockedCounter++;
   	}
	else {


		SYSTEM.TRACK.BINSAT = 0;

		parity_error = 0;
		mode_error = 0;

		for(i = 0, sat_tracked = 0; i < 8; i++){

			// Time solution or position solution
			if((SYSTEM.TRACK.SAT[i].STATUS&0x82)){
	//			if(SYSTEM.TRACK.satmonitor[i] < 10 ) {
	//				SYSTEM.TRACK.satmonitor[i]++;
	//			}

				if(SYSTEM.TRACK.SAT[i].MODE == 8){
					// [01-03-29 4:45��
					tt = 1;
					tt <<= SYSTEM.TRACK.SAT[i].ID;
					SYSTEM.TRACK.BINSAT += tt;

					sat_tracked++;
				}

				if(SYSTEM.TRACK.SAT[i].STATUS&0x59){
					parity_error++;
				}

				if( SYSTEM.TRACK.SAT[i].MODE == 5 ){
					mode_error++;
				}
			}
		}


		// Parity Error
		if( (sat_tracked != 0) && (parity_error >= sat_tracked)){		tr1.parity_error_counter++;	}
		if( parity_error > 0)			{		tr1.parity_1_error_counter++;	}

		// Mode Error
		if( (sat_tracked != 0) && (mode_error >= sat_tracked))	{		tr1.mode_error_counter++;	}
		if( mode_error > 0)				{		tr1.mode_1_error_counter++;	}


		// Tracking
		if( sat_tracked == 0){
			tr1.unlock_counter++;
		}


		//if(FIRST_LOCK || SYSTEM.EB.PHM == ENABLE){
		//	if(sat_tracked==1){
		//		sat_tracked++;
		//	}
		//}
		
		if (SYSTEM.EB.PHM == ENABLE ) {
			position_hold_plus = 1;
		}
		else {
			position_hold_plus = 0;
		}

	    //if((SYSTEM.TRACK.TRACKED >= 2) && (SYSTEM.TRACK.VISIBLE > 0) && (sat_tracked >= 2) ){
	    if((SYSTEM.TRACK.TRACKED+position_hold_plus >= 3) && 
    		(SYSTEM.TRACK.VISIBLE > 0) && 
    		((sat_tracked+position_hold_plus) >= 3) ){
				eb_10min_start = 1;
				if(skipping_change_eb){skipping_change_eb = 0;}
	        	if( CHECKLOCK.LockedCounter < 20 )  CHECKLOCK.LockedCounter++ ;
	    }
	    else {
	        CHECKLOCK.LockedCounter = 0 ;
	    }

	    if(!(SYSTEM.TRACK.STATUS & 0x08) && (SYSTEM.TRACK.DOP == 0)){

	    //if((SYSTEM.EB.PHM != ENABLE) && (SYSTEM.TRACK.DOP == 0)){
	        CHECKLOCK.LockedCounter = 0 ;
	    }
		if(CHECKLOCK.LockedCounter == 0 ){
	        CHECKLOCK.UnlockedCounter++;
		}
		else{
			if(FIRST_LOCK && SYSTEM.EB.STATUS == EB_STATUS_RUN) {
				if(CHECKLOCK.UnlockedCounter>0){
					TLogIt(TLog_No_Sat,CHECKLOCK.UnlockedCounter);
				}
			}
			
	        CHECKLOCK.UnlockedCounter = 0;
		}


	    /*----------------------------------------------------------------
	    ANTENNA FAULT  (fault at minimum 2 seconds successive fault )
	    -----------------------------------------------------------------*/
	    
	    // open
	    //if(SYSTEM.TRACK.DOPTYPE&0x80) {
	    //	if(CONFIG.openshort_flag == ON)
	    //	{
		//    	SYSTEM.EB.ANTENNA = ANTENNA_OPENED;
		//		if( SYSTEM.EB.PrevANTENNA != ANTENNA_OPENED ){  	LogIt(LOG_Antenna_Open,0);	}
		//	}
	    //}
	    // short
	    //else if(SYSTEM.TRACK.DOPTYPE&0x40){
	    //	if(CONFIG.openshort_flag == ON)
	    //	{
		//		SYSTEM.EB.ANTENNA = ANTENNA_SHORTED;
		//		if( SYSTEM.EB.PrevANTENNA != ANTENNA_SHORTED ){  	LogIt(LOG_Antenna_Short,0);	}
		//	}
	    //}
	    // normal
	    //else {
		//	SYSTEM.EB.ANTENNA = OK;
	    //}
        //
		//SYSTEM.EB.PrevANTENNA = SYSTEM.EB.ANTENNA;
		

		//if(CONFIG.AntCheckFlag) {
        //
		//	/*--------------------------------------------------
		//	ANTENNA FAULT  (fault at minimum 5 seconds successive fault )
		//	---------------------------------------------------*/
		//	// open
		//	if(SYSTEM.TRACK.DOPTYPE&0x80) 		{CHECKLOCK.AntennaFaultTable[GC.Second%2] = -1;	}
		//	// short
		//	else if(SYSTEM.TRACK.DOPTYPE&0x40) 	{CHECKLOCK.AntennaFaultTable[GC.Second%2] = 1;	}
		//	else 								{CHECKLOCK.AntennaFaultTable[GC.Second%2] = 0;	}
		//	
		//	
		//	for( i = 0,FaultCounter = 0; i < 2; i++)  FaultCounter += CHECKLOCK.AntennaFaultTable[i];
		//	
		//	switch(FaultCounter){
		//		case	0:
		//					SYSTEM.EB.ANTENNA = OK;
		//		        	DIAG.AntennaFaultCounter = 0;
		//					break;
		//		case	2:
		//					SYSTEM.EB.ANTENNA = ANTENNA_SHORTED;
		//			        if( SYSTEM.EB.PrevANTENNA == OK ){
		//			        	LogIt(LOG_Antenna_Short,0);
		//			        }
		//		        	DIAG.AntennaFaultCounter++;
		//					break;
		//		case	-2:	// Antenna Open � (LG)
		//					SYSTEM.EB.ANTENNA = ANTENNA_OPENED;
		//			        if( SYSTEM.EB.PrevANTENNA == OK ){
		//			        	LogIt(LOG_Antenna_Open,0);
		//			        }
		//		        	DIAG.AntennaFaultCounter++;
		//					break;
		//	}
		//	
		//	SYSTEM.EB.PrevANTENNA = SYSTEM.EB.ANTENNA;
		//}
		
	}


	if((CHECKLOCK.LockedCounter >= 20) && (DIAG.EB != FAIL)) {

        if(SYSTEM.EB.LOCKMODE != EB_LOCKED){
        	LogIt(LOG_CATA1_NORM, LOG_CATA2_OK, LOG_GPS_Lock,0);
        }


        SYSTEM.EB.LOCKMODE = EB_LOCKED;
		gps_holdover_counter = 0;
		gps_detect_flag = ON;

		if(LOCK_REFERENCE){ gps_lock_flag = ON;	}
/*
        // 3d fix
        if( SYSTEM.TRACK.DOPTYPE == 0 && (SYSTEM.TRACK.STATUS>>5)&0x01 ){
        	SYSTEM.EB.LOCKMODE = EB_3D_FIX;
        }

        if ( SYSTEM.EB.PHM == ENABLE ) {
        	SYSTEM.EB.LOCKMODE = EB_POSITION_HOLD;
        }
*/
	}
	else {
		
		gps_holdover_counter++;
		
        //if(SYSTEM.EB.LOCKMODE != EB_UNLOCKED && gps_holdover_counter > 89){	
        //	LogIt(LOG_CATA1_NORM, LOG_CATA2_WARN, LOG_GPS_Unlock,0);        
        //	SYSTEM.EB.LOCKMODE = EB_UNLOCKED;
        //}
        if(gps_holdover_counter > 89){	
        	if(SYSTEM.EB.LOCKMODE != EB_UNLOCKED){
        		LogIt(LOG_CATA1_NORM, LOG_CATA2_WARN, LOG_GPS_Unlock,0);        
        	}
        	SYSTEM.EB.LOCKMODE = EB_UNLOCKED;
        }

        //SYSTEM.EB.LOCKMODE = EB_UNLOCKED;
		
	}




/*
    if( SYSTEM.EB.LOCKMODE == EB_3D_FIX ) {
        if(SYSTEM.POSITION.Survey != SURVEY_END)
            SYSTEM.POSITION.Survey = SURVEY_ON;
    }
    else {
        if(SYSTEM.POSITION.Survey == SURVEY_ON)
            SYSTEM.POSITION.Survey = SURVEY_PAUSE;
    }
*/




//=========================================================
//	E1 Status
//=========================================================
	if(SYSTEM.E1.STATUS == E1_STATUS_RUN ){

		SYSTEM.E1.LOCKMODE = E1_LOCKED;
		e1_holdover_counter = 0;
		e1_detect_flag = ON;
		if(LOCK_REFERENCE){ e1_lock_flag = ON;	}
	}

	else {
		SYSTEM.E1.LOCKMODE = E1_UNLOCKED;
		e1_holdover_counter++;
	}


//=========================================================
//	E1-other Status
//=========================================================
	if(SYSTEM.E2.STATUS == E1_STATUS_RUN ){

		SYSTEM.E2.LOCKMODE = E1_LOCKED;
		e2_holdover_counter = 0;
		e2_detect_flag = ON;
		if(LOCK_REFERENCE){ e2_lock_flag = ON;	}
	}

	else {
		SYSTEM.E2.LOCKMODE = E1_UNLOCKED;
		e2_holdover_counter++;
	}


//=========================================================
//	LMU Status
//=========================================================
	if(lmu.STATUS == LMU_STATUS_RUN ){

		if( lmu.LockedCounter > 0 ){
			lmu.LOCKMODE = LMU_LOCKED;
			lmu_holdover_counter = 0;
			lmu_detect_flag = ON;
			if(LOCK_REFERENCE){ lmu_lock_flag = ON;	}
		}
		else {
			lmu.LOCKMODE = LMU_UNLOCKED;
			lmu_holdover_counter++;
		}
	}
	else {
		lmu.LOCKMODE = LMU_UNLOCKED;
		lmu_holdover_counter++;
	}

//=========================================================
//	Reference
//=========================================================

	holdover_condition = is_holdover_condition();

//	if( (REFERENCE_MODE == UNDEFINED) /* || (holdover_condition == HOLDOVER_OVER10MIN) */) {

		//if((SYSTEM.EB.STATUS == EB_STATUS_RUN) && (SYSTEM.EB.LOCKMODE == EB_LOCKED)){
		if((gps_detect_flag == ON) && (gps_holdover_counter <= Holdover_limit)){
			if(REFERENCE_MODE != EB_STATUS_RUN){
				REFERENCE_MODE = EB_STATUS_RUN;
				Select1PPSEb();

								//RECOVERY.flag = ON;
								//RECOVERY.IntervalCounter = 0;
								//RECOVERY.wait_time = 30;
								//RECOVERY.average_time = 30;
								//Recovery_Offset = 0;

								CONTROL.SYNC = ON;
								control_sync_type = 3;
			}
		}
//		else if( (lmu.STATUS == LMU_STATUS_RUN) && (lmu.LOCKMODE == LMU_LOCKED) ){
		else if((lmu_detect_flag == ON) && (lmu_holdover_counter <= Holdover_limit)){

			if(REFERENCE_MODE != LMU_STATUS_RUN){
				REFERENCE_MODE = LMU_STATUS_RUN;
				/*
				Select1PPSLMU();

								RECOVERY.flag = ON;
								RECOVERY.IntervalCounter = 0;
								RECOVERY.wait_time = 30;
								RECOVERY.average_time = 30;
								Recovery_Offset = 0;

								CONTROL.SYNC = ON;
				*/
			}
		}
//		else if(SYSTEM.E1.STATUS == E1_STATUS_RUN){
		else if((e1_detect_flag == ON) && (e1_holdover_counter <= Holdover_limit)){
			if(REFERENCE_MODE != E1_STATUS_RUN){
				REFERENCE_MODE = E1_STATUS_RUN;
				Select1PPS8K1();

								RECOVERY.flag = ON;
								RECOVERY.IntervalCounter = 0;
								RECOVERY.wait_time = 30;
								RECOVERY.average_time = 30;
								Recovery_Offset = 0;
								//2004-04-30 4:14��
								Init_Offset_2nd();
			}
		}
//		else if(SYSTEM.E2.STATUS == E1_STATUS_RUN){
		else if((e2_detect_flag == ON) && (e2_holdover_counter <= Holdover_limit)){
			if(REFERENCE_MODE != E2_STATUS_RUN){
				/*
				REFERENCE_MODE = E2_STATUS_RUN;
				Select1PPS8K2();

								RECOVERY.flag = ON;
								RECOVERY.IntervalCounter = 0;
								RECOVERY.wait_time = 30;
								RECOVERY.average_time = 30;
								Recovery_Offset = 0;

								Init_Offset_2nd();
				*/
			}
		}
//	}


    if (SYSTEM.IO.CTRL_PORT.MODE == UDEBUG){

		if(LOCK_REFERENCE){		t1 = 'Y';		}
		else {					t1 = 'N';		}

		if(SYSTEM.OPERATION.LOCKMODE == LOCKED){		t2 = 'L';		}
		else {					t2 = 'H';		}

		//if(REFERENCE_MODE == EB_STATUS_RUN){		t3 = 'G';		}
		//else if(REFERENCE_MODE == LMU_STATUS_RUN){		t3 = 'L';		}
		//else if(REFERENCE_MODE == E1_STATUS_RUN){		t3 = 'E';		}
		//else if(REFERENCE_MODE == E2_STATUS_RUN){		t3 = 'e';		}
		//else {					t3 = '.';		}

		if(DIAG.EB_LOCK == OK){t3='L';}
		else{t3='U';}
			
		if((SYSTEM.EB.STATUS == EB_STATUS_RUN) && (SYSTEM.EB.LOCKMODE == EB_LOCKED)){ 	t4 = 'Y';}
		else {																			t4 = 'N';}

		if(lmu.STATUS == LMU_STATUS_RUN){		t5 = 'Y';		}
		else {					t5 = 'N';		}

		if(SYSTEM.E1.STATUS == E1_STATUS_RUN){		t6 = 'Y';		}
		else {					t6 = 'N';		}

		if(SYSTEM.E2.STATUS == E1_STATUS_RUN){		t7 = 'Y';		}
		else {					t7 = 'N';		}

       	if(t4 == 'N') {
       		sprintf((char*)MESG,"<%c[7mG:%c,%c,v:%d,t:%d,%d%c[0m>",
       			0x1B, t4, t3, SYSTEM.TRACK.VISIBLE, SYSTEM.TRACK.TRACKED, CHECKLOCK.LockedCounter, 0x1B);
       	}
       	else {
       		sprintf((char*)MESG,"<G:%c,%c,v:%d,t:%d,%d>",
       			t4, t3,SYSTEM.TRACK.VISIBLE, SYSTEM.TRACK.TRACKED, CHECKLOCK.LockedCounter);
       	}
        TransmitMessage((BYTE*)MESG,DEBUG_PORT);
    }


/*
    if( USER.MANUAL_HOLDOVER ) {
		SYSTEM.OPERATION.LOCKMODE = HOLDOVER;
	}
*/

	holdover_condition = is_holdover_condition();

    switch(SYSTEM.OPERATION.LOCKMODE){
        case    LOCKED:

                    CHECKLOCK.HoldoverCounter = 0;

                    if(holdover_condition != LOCKED ){
                        SYSTEM.OPERATION.LOCKMODE = holdover_condition;
                    }

                    if( CHECKLOCK.HoldoverDelayCounter > 0 ){
                     	CHECKLOCK.HoldoverDelayCounter--;
                    }
                    break;

        case    HOLDOVER:
        case    UNLOCKED:
        case	HOLDOVER_OVER10MIN:

                    CHECKLOCK.HoldoverCounter++;

                    if(holdover_condition == LOCKED){
                    //if(IsRefLocked()){
                    //if( CHECKLOCK.LockedCounter == 20 ){
                        SYSTEM.OPERATION.LOCKMODE = LOCKED;

                        if( CHECKLOCK.HoldoverCounter > 90 ){
                        	CHECKLOCK.HoldoverDelayCounter = 90;
                        }
                        else{
                        	CHECKLOCK.HoldoverDelayCounter = CHECKLOCK.HoldoverCounter;
                        }

						if( FIRST_LOCK /* && (SYSTEM.OPERATION.CTRLMODE == CTRL_LOCKEDtoGPS)*/ ){

							RECOVERY.flag = ON;
							RECOVERY.IntervalCounter = 0;
							//RECOVERY.wait_time = 30;
							//RECOVERY.average_time = 30;
							RECOVERY.wait_time = 100;
							RECOVERY.average_time = 100;

							if((REFERENCE_MODE == E1_STATUS_RUN) || (REFERENCE_MODE == E2_STATUS_RUN)){
								Recovery_Offset = 0;
							}

						}
                    }

					else {
						SYSTEM.OPERATION.LOCKMODE = holdover_condition;
					}

                    break;
    }
}




void gwait(int delay)
{
	
    int i,j;

    for(j = 0 ; j < 3900 ; j++)
        for(i = 0 ; i < delay; i++)
        ;
    
    /*
    _tick1 = delay;
    while(_tick1>0);
    */
}



unsigned short CheckCRC16(unsigned char *Buffer, int length)
{
	register unsigned short	tmp, CRC16;
	register int i = 0;

	for(CRC16 = 0; i < length; i++){
		tmp 	= (CRC16 >> 8) ^ Buffer[i];
		CRC16 	= (CRC16 << 8) ^ CRC16TABLE[tmp];
	}
    CRC16 = (CRC16<<8&0xFF00) | (CRC16>>8&0x00FF) ; //kang
	return CRC16;
}

unsigned short CheckCRC16_C(unsigned char *Buffer, int length, unsigned short CRC16)
{
	int i;
	register unsigned short	tmp;

	for(i = 0; i < length; i++){
		tmp 	= (CRC16 >> 8) ^ Buffer[i];
		CRC16 	= (CRC16 << 8) ^ CRC16TABLE[tmp];
	}

	return CRC16;
}


void reset_system()
{


	UnconditionalSigOff();
	gwait(100);
}

void reset_eb_counter()
{

	tr1.parity_error_counter =
	tr1.mode_error_counter =
	tr1.parity_1_error_counter =
	tr1.mode_1_error_counter =
	tr1.unlock_counter = 0;
}


void EB_STATUS_OUT()
{
	int i, offset;

	sprintf(MESG,"x[1;1HDT: %.2X V: %3d T: %3d ST: %.2X\r\n",
					SYSTEM.TRACK.DOPTYPE,SYSTEM.TRACK.VISIBLE,SYSTEM.TRACK.TRACKED,SYSTEM.TRACK.STATUS);

	MESG[0] = 0x1B;
	TransmitMessage((BYTE*)MESG,PORT_CONTROL);
	
	for( i = 0; i < 8; i++ ){

		offset = i*28;

		MESG[offset + 0]	= 'I';
		MESG[offset + 1]	= 'D';
		MESG[offset + 2]	= ':';
		MESG[offset + 3]	= ' ';
		MESG[offset + 4]   =	hex_char[(SYSTEM.TRACK.SAT[i].ID >> 4) & 0xF];
		MESG[offset + 5]   =	hex_char[ SYSTEM.TRACK.SAT[i].ID  	 & 0xF];

		MESG[offset + 6]	= ' ';
		MESG[offset + 7]	= 'S';
		MESG[offset + 8]	= 'S';
		MESG[offset + 9]	= ':';
		MESG[offset + 10]	= ' ';

		MESG[offset + 11]   =	hex_char[(SYSTEM.TRACK.SAT[i].SS >> 4) & 0xF];
		MESG[offset + 12]   =	hex_char[ SYSTEM.TRACK.SAT[i].SS  	 & 0xF];

		MESG[offset + 13]	= ' ';
		MESG[offset + 14]	= 'M';
		MESG[offset + 15]	= ':';
		MESG[offset + 16]	= ' ';

		MESG[offset + 17]   =	hex_char[(SYSTEM.TRACK.SAT[i].MODE >> 4) & 0xF];
		MESG[offset + 18]   =	hex_char[ SYSTEM.TRACK.SAT[i].MODE       & 0xF];

		MESG[offset + 19]	= ' ';
		MESG[offset + 20]	= 'S';
		MESG[offset + 21]	= 'T';
		MESG[offset + 22]	= ':';
		MESG[offset + 23]	= ' ';

		MESG[offset + 24]   =	hex_char[(SYSTEM.TRACK.SAT[i].STATUS >> 4) & 0xF];
		MESG[offset + 25]   =	hex_char[ SYSTEM.TRACK.SAT[i].STATUS  	 & 0xF];

		MESG[offset + 26]	= 0x0D;
		MESG[offset + 27]	= 0x0A;
	}
	//MESG[offset+29] = NULL;
	//kang TransmitnMessage((BYTE*)MESG,offset+27,PORT_CONTROL);
	
	write(STDOUT_FILENO, MESG, 224);  //kang 224 = 8*28
	

	sprintf(MESG,"\r\n\r\nUnlock:%4d  Mode:%3d %4d  Parity:%4d %4d\r\n---------------- %9d ------------------\r\n\r\n",
				tr1.unlock_counter, tr1.mode_1_error_counter,tr1.mode_error_counter,
									tr1.parity_1_error_counter,tr1.parity_error_counter, SYSTEM.EB.RxCheckCounter);
	TransmitMessage((BYTE*)MESG,PORT_CONTROL);
	
}



char bufMonitorEB[500];
void EB_STATUS_OUT_kdy()
{
	int i,offset;

	printf("%c[1;1HDT: %.2X V: %3d T: %3d ST: %.2X\r\n",0x1B,
					SYSTEM.TRACK.DOPTYPE,SYSTEM.TRACK.VISIBLE,SYSTEM.TRACK.TRACKED,SYSTEM.TRACK.STATUS);
					

	for( i = 0; i < 8; i++ ){

		//offset = i*28;
		offset = 0*28;

		bufMonitorEB[offset + 0]	= 'I';
		bufMonitorEB[offset + 1]	= 'D';
		bufMonitorEB[offset + 2]	= ':';
		bufMonitorEB[offset + 3]	= ' ';
		bufMonitorEB[offset + 4]   =	hex_char[(SYSTEM.TRACK.SAT[i].ID >> 4) & 0xF];
		bufMonitorEB[offset + 5]   =	hex_char[ SYSTEM.TRACK.SAT[i].ID  	 & 0xF];

		bufMonitorEB[offset + 6]	= ' ';
		bufMonitorEB[offset + 7]	= 'S';
		bufMonitorEB[offset + 8]	= 'S';
		bufMonitorEB[offset + 9]	= ':';
		bufMonitorEB[offset + 10]	= ' ';

		bufMonitorEB[offset + 11]   =	hex_char[(SYSTEM.TRACK.SAT[i].SS >> 4) & 0xF];
		bufMonitorEB[offset + 12]   =	hex_char[ SYSTEM.TRACK.SAT[i].SS  	 & 0xF];

		bufMonitorEB[offset + 13]	= ' ';
		bufMonitorEB[offset + 14]	= 'M';
		bufMonitorEB[offset + 15]	= ':';
		bufMonitorEB[offset + 16]	= ' ';

		bufMonitorEB[offset + 17]   =	hex_char[(SYSTEM.TRACK.SAT[i].MODE >> 4) & 0xF];
		bufMonitorEB[offset + 18]   =	hex_char[ SYSTEM.TRACK.SAT[i].MODE       & 0xF];

		bufMonitorEB[offset + 19]	= ' ';
		bufMonitorEB[offset + 20]	= 'S';
		bufMonitorEB[offset + 21]	= 'T';
		bufMonitorEB[offset + 22]	= ':';
		bufMonitorEB[offset + 23]	= ' ';

		bufMonitorEB[offset + 24]   =	hex_char[(SYSTEM.TRACK.SAT[i].STATUS >> 4) & 0xF];
		bufMonitorEB[offset + 25]   =	hex_char[ SYSTEM.TRACK.SAT[i].STATUS  	 & 0xF];

		bufMonitorEB[offset + 26]	= 0x0D;
		bufMonitorEB[offset + 27]	= 0x0A;
		printf(bufMonitorEB);
	}
	//MESG[offset+29] = NULL;
	//TransmitnMessage((BYTE*)MESG,offset+27,PORT_CONTROL);

	printf("\r\n\r\nUnlock:%4d  Mode:%3d %4d  Parity:%4d %4d\r\n--------------------------------------------\r\n\r\n",
				tr1.unlock_counter, tr1.mode_1_error_counter,tr1.mode_error_counter,
									tr1.parity_1_error_counter,tr1.parity_error_counter);
									
}

